package com.alianzamedica.view;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.objectsearch.web.tools.Converter;
import org.objectsearch.web.tools.FormatDateTool;

import com.alianzamedica.businessobject.Patient;
import com.alianzamedica.tools.ValidateTool;

/**
 * @author Carlos
 * 
 */
public class PatientForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5291774330061668522L;

	private String id;
	private String firstName;
	private String lastName;
	private String card;
	private String age;
	private String since;
	private String comment;
	private String location;
	private String diagnostic;// 'PATIENT_DIAGNOSTIC', 'varchar(1000)', 'NO',
	private String sex; // PATIENT_SEX
	private String weight; // PATIENT_WEIGHT
	private String phone; // PATIENT_PHONE
	private String email; // PATIENT_EMAIL

	/**
	 * regresa el id generado.
	 * 
	 * @return regresa el id generado.
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * nombre.
	 * 
	 * @return nombre.
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * nombre.
	 * 
	 * @param firstName
	 *            nombre.
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * apellido.
	 * 
	 * @return apellido.
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * apellido.
	 * 
	 * @param lastName
	 *            apellido.
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * tarjeta.
	 * 
	 * @return tarjeta.
	 */
	public String getCard() {
		return card;
	}

	/**
	 * tarjeta.
	 * 
	 * @param card
	 *            tarjeta.
	 */
	public void setCard(String card) {
		this.card = card;
	}

	/**
	 * edad.
	 * 
	 * @return edad.
	 */
	public String getAge() {
		return age;
	}

	/**
	 * edad.
	 * 
	 * @param age
	 *            edad.
	 */
	public void setAge(String age) {
		this.age = age;
	}

	/**
	 * desde.
	 * 
	 * @return desde.
	 */
	public String getSince() {
		return since;
	}

	/**
	 * desde.
	 * 
	 * @param since
	 */
	public void setSince(String since) {
		this.since = since;
	}

	/**
	 * comentario.
	 * 
	 * @return comentario.
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * comentario.
	 * 
	 * @param comment
	 *            comentario.
	 */
	public void setComment(String comment) {
		this.comment = comment;
	}

	/**
	 * localizacion.
	 * 
	 * @return localizacion.
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * localizacion.
	 * 
	 * @param location
	 *            localizacion.
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * diagnostico.
	 * 
	 * @return diagnostico.
	 */
	public String getDiagnostic() {
		return diagnostic;
	}

	/**
	 * diagnostico.
	 * 
	 * @param diagnostic
	 *            diagnostico.
	 */
	public void setDiagnostic(String diagnostic) {
		this.diagnostic = diagnostic;
	}

	/**
	 * sexo.
	 * 
	 * @return sexo.
	 */
	public String getSex() {
		return sex;
	}

	/**
	 * sexo.
	 * 
	 * @param sex
	 *            sexo.
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}

	/**
	 * peso.
	 * 
	 * @return peso.
	 */
	public String getWeight() {
		return weight;
	}

	/**
	 * peso.
	 * 
	 * @param weight
	 *            peso.
	 */
	public void setWeight(String weight) {
		this.weight = weight;
	}

	/**
	 * telefono.
	 * 
	 * @return telefono.
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * telefono.
	 * 
	 * @param phone
	 *            telefono.
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * email.
	 * 
	 * @param email
	 *            email.
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * email.
	 * 
	 * @return email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * paciente.
	 * 
	 * @return paciente.
	 */
	public Patient getPatient() {
		Patient patient = new Patient();
		patient.setId(Converter.string2Integer(id));
		patient.setFirstName(firstName);
		patient.setLastName(lastName);
		patient.setCard(card);
		try {
			patient.setAge(Converter.string2Integer(age));
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			patient.setSince(Converter.string2Date(since));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		patient.setComment(comment);
		patient.setLocation(location);
		patient.setDiagnostic(diagnostic);
		patient.setSex(sex);
		try {
			patient.setWeight(Converter.string2Float(weight));
		} catch (Exception e) {
			e.printStackTrace();
		}
		patient.setPhone(phone);
		patient.setEmail(email);

		return patient;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		ValidateTool validateTool = new ValidateTool();
		if (validateTool.isStringEmpty(firstName)) {
			errors.add("firstName", new ActionMessage("Nombre es requerido"));
		}
		if (validateTool.isStringEmpty(lastName)) {
			errors.add("lastName", new ActionMessage("Apellido es requerido"));
		}
		if (validateTool.isStringEmpty(card)) {
			errors.add("card", new ActionMessage("Tarjeta es requerido"));
		}
		if (!validateTool.isNumeric(age)) {
			errors.add("age", new ActionMessage("Edad tiene valor invalido"));
		}
		if (!validateTool.isDate(since)) {
			errors.add("since", new ActionMessage(
					" Fecha de alta no es una fecha valida"));
		}
		if (!validateTool.isFloat(weight)) {
			errors.add("weight", new ActionMessage("El peso no es valido"));
		}
		if (validateTool.isStringEmpty(email)) {
			errors.add("email", new ActionMessage("El Email es requerido"));
		} else if (!validateTool.isEmail(email)) {
			errors.add("email", new ActionMessage(
					"El valor del Email no es correcto"));
		}
		if (errors.size() > 0) {
			request.setAttribute("error", errors);
			Patient patient = this.getPatient();
			request.setAttribute("patient", patient);
			FormatDateTool format = new FormatDateTool();
			request.setAttribute("format", format);
		}
		return errors;
	}

}
